package com.talent.codes.model;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.freewayso.image.combiner.ImageCombiner;
import com.freewayso.image.combiner.enums.OutputFormat;
import com.freewayso.image.combiner.enums.ZoomMode;
import com.google.zxing.BarcodeFormat;

import com.talent.codes.utils.CodeImageGenerator;

public class GasIdCtrl {
    protected GasLabel gasLabel;
    protected long firstId;
    protected long currId = Long.MIN_VALUE;
    protected long lastId;
    protected int idCnt;
    protected String currUrl;

    private byte[] qrCode;
    private byte[] barCode;

    public GasIdCtrl(GasLabel label, long firstId, int idCnt) {
        this.gasLabel = label;
        this.firstId = firstId;
        this.idCnt = idCnt;
        this.lastId = firstId + idCnt - 1;
    }


    public boolean hasNext(){
        return currId<lastId;
    }

    public void next() throws Exception{
        if(currId>=lastId){
            throw new Exception("已经是最后一个标签.");
        }
        if(currId < 0){
            currId = firstId;
        }else{
            currId++;
        }
        currUrl = this.gasLabel.webSite + currId;

        // 不要太过密密麻麻，像素可以低一点
        ElePos qrCodePos = this.gasLabel.qrCodePos;
        ElePos barCodePos = this.gasLabel.barCodePos;

        int min = Math.min(qrCodePos.getWidth(), qrCodePos.getHeight()) - 4;
        //double mind = Double.valueOf(Math.sqrt(min)).intValue();Double.valueOf(Math.pow(mind,2)).intValue()
        //min = 48;
        int qrW = min;
        int qrH = qrW;
        qrCode = CodeImageGenerator.genQRCode(currUrl, qrW, qrH);
        barCode = CodeImageGenerator.genBarCode(currId+"", BarcodeFormat.CODE_128,
                barCodePos.getWidth() - 30, barCodePos.getHeight()-10);

    }

    public byte[] curLabelData() throws Exception{
        BufferedImage qrBi = ImageIO.read(new ByteArrayInputStream(qrCode));
        BufferedImage barBi = ImageIO.read(new ByteArrayInputStream(barCode));

        ImageCombiner combiner = new ImageCombiner(gasLabel.width, gasLabel.height, Color.WHITE, OutputFormat.JPG);  //v1.1.4之后可以指定背景图新宽高了（不指定则默认用图片原宽高）
        combiner.setQuality(1.0f);//设置图片保存质量（0.0~1.0，Java9以下仅jpg格式有效）

        EleStyle titleStyle = gasLabel.titleStyle;
        ElePos titlePos = gasLabel.titlePos;
        ElePos qrCodePos = gasLabel.qrCodePos;
        ElePos barCodePos = gasLabel.barCodePos;
        EleStyle barCodeStyle = gasLabel.barCodeStyle;

        //内容（设置文本自动换行，需要指定最大宽度（超出则换行）、最大行数（超出则丢弃）、行高）
        combiner.addTextElement(gasLabel.title, titleStyle.getFontName(), titleStyle.getFontSize(),
                        titlePos.getX(), titlePos.getY()).setSpace(titleStyle.getSpace())
                .setAutoBreakLine(titlePos.getWidth(), 2, titleStyle.getLineHeight());//自动换行（还有一个LineAlign参数可以指定对齐方式）

        //140*140
        //二维码（强制按指定宽度、高度缩放）
        combiner.addImageElement( qrBi, qrCodePos.getX(), qrCodePos.getY(),
                qrCodePos.getWidth(), qrCodePos.getHeight(), ZoomMode.WidthHeight);

        //65的高度
        //条形码（强制按指定宽度、高度缩放）
        //combiner.addImageElement( barBi, -10, 144,
        //        260, 65, ZoomMode.Width);

        combiner.addImageElement( barBi, barCodePos.getX(), barCodePos.getY(),
                barCodePos.getWidth(), barCodePos.getHeight(), ZoomMode.Width);

        //30左右的高度
        //内容（设置文本自动换行，需要指定最大宽度（超出则换行）、最大行数（超出则丢弃）、行高）
        int nheight = barCodePos.getY() + barCodePos.getHeight() - 10;
        combiner.addTextElement(currId+"", barCodeStyle.getFontSize(), 6, nheight);

        //合成图片
        combiner.combine();
        InputStream cbi = combiner.getCombinedImageStream();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write(cbi.readAllBytes());
        return os.toByteArray();
    }


    public long currId(){
        return currId;
    }

    public String currUrl(){
        if(currId<0){
            return this.gasLabel.webSite;
        }else{
            return currUrl;
        }
    }


    public byte[] currQrCode() throws IOException {
        if(qrCode == null) {
            throw new IOException("qrCode is null");
        }
        return qrCode;
    }

    public byte[] currBarCode() throws Exception {
        if(barCode == null) {
            throw new IOException("barCode is null");
        }
        return barCode;
    }



    public long getFirstId() {
        return firstId;
    }

    public int getIdCnt() {
        return idCnt;
    }

    public long lastId() {
        return lastId;
    }

}
